#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This script downloads and installs IBM MQ client libraries
# required for the ibm.d.plugin MQ PCF collector to work.
#
# Note: Database collectors (AS/400, DB2) now use unixODBC exclusively
# and do not require IBM DB2 client libraries.

set -e

MQ_CLIENT_URL="https://public.dhe.ibm.com/ibmdl/export/pub/software/websphere/messaging/mqdev/redist/9.4.1.0-IBM-MQC-Redist-LinuxX64.tar.gz"

# Set installation directory from CMake
BASE_DIR="@NETDATA_RUNTIME_PREFIX@/usr/lib/netdata"

# Allow override via command line
if [ -n "$1" ]; then
    BASE_DIR="$1"
    echo "Using specified installation directory: $BASE_DIR"
else
    echo "Using Netdata installation directory: $BASE_DIR"
fi

MQ_CLIENT_DIR="${BASE_DIR}/ibm-mqclient"

# Check if already installed
if [ -f "$MQ_CLIENT_DIR/lib64/libmqm.so" ]; then
    echo "IBM MQ client libraries are already installed"
    echo "  Path: $MQ_CLIENT_DIR"
    exit 0
fi

# Check for root permissions
if [ "$(id -u)" -ne 0 ]; then
    echo "ERROR: This script must be run as root"
    exit 1
fi

# Install MQ libraries
echo "Installing IBM MQ client libraries to: $MQ_CLIENT_DIR"
mkdir -p "$MQ_CLIENT_DIR"
cd "$MQ_CLIENT_DIR"

# Download
echo "Downloading IBM MQ client libraries..."
if command -v wget >/dev/null 2>&1; then
    wget -q --show-progress -O mqclient.tar.gz "$MQ_CLIENT_URL" || {
        echo "ERROR: Failed to download IBM MQ client libraries"
        exit 1
    }
elif command -v curl >/dev/null 2>&1; then
    curl -# -L -o mqclient.tar.gz "$MQ_CLIENT_URL" || {
        echo "ERROR: Failed to download IBM MQ client libraries"
        exit 1
    }
else
    echo "ERROR: Neither wget nor curl found. Cannot download IBM libraries."
    echo "Please install wget or curl and try again."
    exit 1
fi

# Extract
echo "Extracting IBM MQ client libraries..."
tar -xzf mqclient.tar.gz || {
    echo "ERROR: Failed to extract IBM MQ client libraries"
    rm -f mqclient.tar.gz
    exit 1
}
rm -f mqclient.tar.gz

# Set proper permissions
chown -R root:root "$MQ_CLIENT_DIR"
chmod -R 755 "$MQ_CLIENT_DIR"
echo "IBM MQ client libraries installed successfully."
echo "  Installation path: $MQ_CLIENT_DIR"
echo "  Headers: $MQ_CLIENT_DIR/inc"
echo "  Libraries: $MQ_CLIENT_DIR/lib64"
echo "  Binaries: $MQ_CLIENT_DIR/bin"

echo ""
echo "=========================================="
echo "IBM MQ Client Libraries Installation Summary"
echo "=========================================="
echo ""
echo "Base installation directory: $BASE_DIR"
echo ""
if [ -f "$MQ_CLIENT_DIR/lib64/libmqm.so" ]; then
    echo "MQ Client Libraries:"
    echo "  Path: $MQ_CLIENT_DIR"
    echo "  Headers: $MQ_CLIENT_DIR/inc"
    echo "  Libraries: $MQ_CLIENT_DIR/lib64"
fi
echo ""
echo "The ibm.d.plugin should now be able to connect to:"
echo "  - IBM MQ queue managers"
echo "  - IBM WebSphere Application Server (via MQ)"
echo ""
echo "For database monitoring (DB2, AS/400), install unixODBC and appropriate ODBC drivers:"
echo "  - unixODBC: apt-get install unixodbc (Ubuntu) or yum install unixODBC (RHEL)"
echo "  - IBM DB2 ODBC Driver: Download from IBM Data Server Driver Package"
echo "  - IBM i Access ODBC Driver: Download from IBM i Access Client Solutions"
echo ""
echo "Note: You may need to restart the Netdata service for the changes to take effect:"
echo "  systemctl restart netdata    # For systemd systems"
echo "  /etc/init.d/netdata restart  # For SysV init systems"